/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gr_graph_fun.h>
#include "GR_write_logml.h"

FILE *
assert_open_xml_file(const char *name_file)
{
  FILE *fp;
  if(!name_file)
    {
      fprintf(stderr, "LOGML File name is missing\n");
      return NULL;
    }
  fp = fopen(name_file, "w+");
  if(!fp)
    {
      fprintf(stderr, "Can't open %s \n",name_file);
      return NULL;
    }
  return fp;
}

void
GR_write_xml_head_logml_file(FILE *fp)
{
    fprintf(fp, XML_VERSION_STRING);
    fprintf(fp, DOCTYPE_STRING);
    fprintf(fp, LOGML_GENERATOR);
}

void
GR_write_head_logml_file(FILE *fp,char *start_date, char *end_date)
{
    fprintf(fp,"<logml");
    if(start_date && end_date)
       fprintf(fp, " start_date=\"%s\" end_date=\"%s\"", start_date, end_date);
    fprintf(fp,">\n");
}

void
GR_write_end_logml_file(FILE *fp)
{
  fprintf(fp,"</logml>\n");
}

void
GR_write_start_graph_logml_file(FILE *fp)
{
  fprintf(fp,"<graph directed=\"1\">\n");
}

void
GR_write_end_graph_logml_file(FILE *fp)
{
  fprintf(fp,"</graph>\n");
}

void
GR_write_start_node_logml_file(FILE *fp,int nid,char *slabel,int nhits,int isindp,int ehits)
{
    fprintf(fp,"<node id=\"%d\" label=\"%s\" hits=\"%d\" weight=\"%d\"",nid,slabel,nhits,nhits);
    if(isindp == 1)
	fprintf(fp," indp=\"1\"");
    if(ehits > 0)
	fprintf(fp," ehits=\"%d\"",ehits);
    fprintf(fp,">\n");

}

void
GR_write_end_node_logml_file(FILE *fp)
{
    fprintf(fp,"</node>\n");
}

void
GR_write_node_att_logml_file(FILE *fp,char *aname, char *avalue)
{
    fprintf(fp,"<att name=\"%s\" value=\"%s\"/>\n",aname,avalue);
}

void
GR_write_empty_edge_logml_file(FILE *fp,int sid,int tid,char *elabel,int hits,int isindp)
{
    if(elabel)
	fprintf(fp,
		"<edge source=\"%d\" target=\"%d\" label=\"%s\" hits=\"%d\" weight=\"%d\"",
		sid,tid,elabel,hits,hits);
    else
	fprintf(fp,
		"<edge source=\"%d\" target=\"%d\" hits=\"%d\" weight=\"%d\"",
		sid,tid,hits,hits);

    if(isindp == 1)
	fprintf(fp," indp=\"1\"");

    fprintf(fp,"/>\n");
}


void
GR_write_start_edge_logml_file(FILE *fp,int sid,int tid,char *elabel,int hits,int isindp)
{
    if(elabel)
	fprintf(fp,
		"<edge source=\"%d\" target=\"%d\" label=\"%s\" hits=\"%d\" weight=\"%d\"",
		sid,tid,elabel,hits,hits);
    else
	fprintf(fp,
		"<edge source=\"%d\" target=\"%d\" hits=\"%d\" weight=\"%d\"",
		sid,tid,hits,hits);

    if(isindp == 1)
	fprintf(fp," indp=\"1\"");

    fprintf(fp,">\n");
}


void
GR_write_end_edge_logml_file(FILE *fp)
{
    fprintf(fp,"</edge>\n");
}

void
GR_write_att_edge_logml_file(FILE *fp,char *satt)
{
    fprintf(fp,"<att value=\"%s\"/>\n",satt);
}

void
GR_write_start_hosts_logml_file(FILE *fp, int count)
{
    fprintf(fp,"<hosts count=\"%d\">\n",count);
}

void
GR_write_end_hosts_logml_file(FILE *fp)
{
    fprintf(fp,"</hosts>\n");
}

void
GR_write_host_logml_file(FILE *fp,char *name,char *ip,long acount,long bytes,
			 long npages,int is_internal)
{
    fprintf(fp,"<host");
    if(name)
	fprintf(fp," name=\"%s\"",name);
    if(ip)
	fprintf(fp," ip=\"%s\"",ip);
    fprintf(fp, " access_count=\"%ld\" bytes=\"%ld\" html_pages=\"%ld\"",
	    acount,bytes,npages);
    if(is_internal)
	fprintf(fp," is_internal=\"%d\"",is_internal);
    fprintf(fp,"/>\n");
}


void
GR_write_start_domains_logml_file(FILE *fp, int count)
{
    fprintf(fp,"<domains count=\"%d\">\n",count);
}

void
GR_write_end_domains_logml_file(FILE *fp)
{
    fprintf(fp,"</domains>\n");
}

void
GR_write_domain_logml_file(FILE *fp,char *name,long acount,
			   char *sbytes,long npages )
{
    fprintf(fp,"<domain");
    if(name)
	fprintf(fp," name=\"%s\"",name);
    fprintf(fp, " access_count=\"%ld\" bytes=\"%s\" html_pages=\"%ld\"",
	    acount,sbytes,npages);
    fprintf(fp,"/>\n");
}

void
GR_write_start_directories_logml_file(FILE *fp, int count)
{
    fprintf(fp,"<directories count=\"%d\">\n",count);
}

void
GR_write_end_directories_logml_file(FILE *fp)
{
    fprintf(fp,"</directories>\n");
}

void
GR_write_directory_logml_file(FILE *fp,char *name,long acount,long tcount, long bytes)
{
    fprintf(fp,"<directory");
    if(name)
	fprintf(fp," name=\"%s\"",name);
    fprintf(fp, " access_count=\"%ld\" total_count=\"%ld\" bytes=\"%ld\"",
	    acount,tcount,bytes);
    fprintf(fp,"/>\n");
}

void
GR_write_start_user_agents_logml_file(FILE *fp, int count)
{
    fprintf(fp,"<userAgents count=\"%d\">\n",count);
}

void
GR_write_end_user_agents_logml_file(FILE *fp)
{
    fprintf(fp,"</userAgents>\n");
}

void
GR_write_user_agent_logml_file(FILE *fp,char *name,long acount,double bytes,
			       long npages)
{
    fprintf(fp,"<userAgent");
    if(name)
	fprintf(fp," name=\"%s\"",name);
    fprintf(fp, " access_count=\"%ld\" bytes=\"%.0f\" html_pages=\"%ld\"",
	    acount,bytes,npages);
    fprintf(fp,"/>\n");
}

void
GR_write_start_host_referers_logml_file(FILE *fp, int count)
{
    fprintf(fp,"<hostReferers count=\"%d\">\n",count);
}

void
GR_write_end_host_referers_logml_file(FILE *fp)
{
    fprintf(fp,"</hostReferers>\n");
}

void
GR_write_host_referer_logml_file(FILE *fp,char *name,long acount,long bytes,
				 int isse)
{
    fprintf(fp,"<hostReferer");
    if(name)
	fprintf(fp," name=\"%s\"",name);
    fprintf(fp, " access_count=\"%ld\" bytes=\"%ld\"",acount,bytes);
    if(isse == 1)
	fprintf(fp," is_search_engine=\"1\"");
    fprintf(fp,"/>\n");
}


void
GR_write_start_referers_logml_file(FILE *fp, int count)
{
    fprintf(fp,"<referers count=\"%d\">\n",count);
}

void
GR_write_end_referers_logml_file(FILE *fp)
{
    fprintf(fp,"</referers>\n");
}

void
GR_write_referer_logml_file(FILE *fp,char *name,long acount,long bytes,
				 int isinternal,int target,int http_code)
{
    fprintf(fp,"<referer");
    if(name)
	fprintf(fp," name=\"%s\"",name);
    fprintf(fp, " access_count=\"%ld\" bytes=\"%ld\"",acount,bytes);
    if(isinternal == 1)
	fprintf(fp," is_internal=\"1\"");
    if(target >= 0)
	fprintf(fp," target=\"%d\"",target);
    if(http_code >= 0)
	fprintf(fp," http_code=\"%d\"",http_code);
    fprintf(fp,"/>\n");
}

void
GR_write_start_keywords_logml_file(FILE *fp, int count,char *search_engine_name, long search_count)
{
    fprintf(fp,"<keywords count=\"%d\"",count);
    if(search_engine_name)
	fprintf(fp," search_engine_name=\"%s\"",search_engine_name);
    fprintf(fp, " search_count=\"%ld\"",search_count);
    fprintf(fp,">\n");
}

void
GR_write_end_keywords_logml_file(FILE *fp)
{
    fprintf(fp,"</keywords>\n");
}

void
GR_write_keyword_logml_file(FILE *fp,char *name,int acount)
{
    fprintf(fp,"<keyword");
    if(name)
	fprintf(fp," name=\"%s\"",name);
    fprintf(fp, " total_count=\"%d\"/>\n",acount);
}

void
GR_write_start_summary_logml_file(FILE *fp,
			    long requests, long sessions,long bytes, 
			    long html_pages, long nhtml_pages, 
			    long inline_objects, long hyperlink_html, 
			    long hyperlink_nhtml, long html_entry_pages,
			    long nhtml_entry_pages, long unique_sites,
			    long unique_host_referers,long unique_se_referers,
			    long unique_external_url_referers,
			    long unique_internal_url_referers,
			    long unique_user_agents,
			    double requests_hour, double requests_day,
			    double kbytes_day, double kbytes_hour, 
			    long searches, long unique_keywords)
{
    fprintf(fp,"<summary \n");
    if(requests>=0)
	fprintf(fp," requests=\"%ld\"",requests);
    if(sessions>=0)
	fprintf(fp," sessions=\"%ld\"",sessions);
    if(bytes>=0)
	fprintf(fp," bytes=\"%ld\"\n",bytes);
    if(html_pages>=0)
	fprintf(fp," html_pages=\"%ld\"",html_pages);
    if(nhtml_pages>=0)
	fprintf(fp," nhtml_pages=\"%ld\"",nhtml_pages);
    if(inline_objects>=0)
	fprintf(fp," inline_objects=\"%ld\"",inline_objects);
    if(hyperlink_html>=0)
	fprintf(fp," hyperlink_html=\"%ld\"",hyperlink_html);
    if(hyperlink_nhtml>=0)
	fprintf(fp," hyperlink_nhtml=\"%ld\"\n",hyperlink_nhtml);
    if(html_entry_pages>=0)
	fprintf(fp," html_entry_pages=\"%ld\"",html_entry_pages);
    if(nhtml_entry_pages>=0)
	fprintf(fp," nhtml_entry_pages=\"%ld\"",nhtml_entry_pages);
    if(unique_sites>=0)
	fprintf(fp," unique_sites=\"%ld\"",unique_sites);
    if(unique_host_referers>=0)
	fprintf(fp," unique_host_referers=\"%ld\"",unique_host_referers);
    if(unique_se_referers>=0)
	fprintf(fp," unique_se_referers=\"%ld\"\n",unique_se_referers);
    if(unique_external_url_referers>=0)
	fprintf(fp," unique_external_url_referers=\"%ld\"",unique_external_url_referers);
    if(unique_internal_url_referers>=0)
	fprintf(fp," unique_internal_url_referers=\"%ld\"",unique_internal_url_referers);
    if(unique_user_agents>=0)
	fprintf(fp," unique_user_agents=\"%ld\"\n",unique_user_agents);

      if(requests_hour>=0)
	  fprintf(fp," requests_hour=\"%.2f\"",requests_hour);
      if(requests_day>=0)
	  fprintf(fp," requests_day=\"%.2f\"",requests_day);
      if(kbytes_day>=0)
	  fprintf(fp," kbytes_day=\"%.2f\"",kbytes_day/1024.0);
      if(kbytes_hour>=0)
	  fprintf(fp," kbytes_hour=\"%.2f\"\n",kbytes_hour/1024.0);

    if(searches>=0)
	fprintf(fp," searches=\"%ld\"",searches);
    if(unique_keywords>=0)
	fprintf(fp," unique_keywords=\"%ld\"",unique_keywords);
    fprintf(fp,">\n");
}

void
GR_write_end_summary_logml_file(FILE *fp)
{
    fprintf(fp,"</summary>\n");
}

void
GR_write_http_code_logml_file(FILE *fp,int code,char *name,long acount,
			      double bytes, long npages)
{
    fprintf(fp,"<httpCode code=\"%d\"",code);
    if(name)
	fprintf(fp," name=\"%s\"",name);
    fprintf(fp, " total_count=\"%ld\" bytes=\"%.0f\" html_pages=\"%ld\"/>\n",
	    acount,bytes,npages);
}

void
GR_write_http_method_logml_file(FILE *fp,char *name,long acount,
			      double bytes, long npages)
{
    if(!name)
	return;
    fprintf(fp,"<httpMethod name=\"%s\"",name);
    fprintf(fp, " total_count=\"%ld\" bytes=\"%.0f\" html_pages=\"%ld\"/>\n",
	    acount,bytes,npages);
}

void
GR_write_http_version_logml_file(FILE *fp,char *name,long acount,
				 double bytes, long npages)
{
    if(!name)	
	return;
    fprintf(fp,"<httpVersion name=\"%s\"",name);
    fprintf(fp, " total_count=\"%ld\" bytes=\"%.0f\" html_pages=\"%ld\"/>\n",
	    acount,bytes,npages);
}

void
GR_write_start_date_stat_logml_file(FILE *fp)
{
    fprintf(fp,"<dateStat>\n");
}


void
GR_write_end_date_stat_logml_file(FILE *fp)
{
    fprintf(fp,"</dateStat>\n");
}

void
GR_write_month_stat_logml_file(FILE *fp,int month,long hits,long bytes,
			  long html_pages)
{
    fprintf(fp,"<monthStat month=\"%d\" hits=\"%ld\" bytes=\"%ld\" html_pages=\"%ld\"/>\n",
	    month,hits,bytes,html_pages);
}


void
GR_write_day_stat_logml_file(FILE *fp,int day,long hits,long bytes,
			  long html_pages)
{
    fprintf(fp,"<dayStat day=\"%d\" hits=\"%ld\" bytes=\"%ld\" html_pages=\"%ld\"/>\n",
	    day,hits,bytes,html_pages);
}

void
GR_write_hour_stat_logml_file(FILE *fp,int hour,long hits,long bytes,
			  long html_pages)
{
    fprintf(fp,"<hourStat hour=\"%d\" hits=\"%ld\" bytes=\"%ld\" html_pages=\"%ld\"/>\n",
	    hour,hits,bytes,html_pages);
}

void
GR_write_empty_uedge_logml_file(FILE *fp,int sid,int tid,char *t)
{
    fprintf(fp,"<uedge source=\"%d\" target=\"%d\" utime=\"%s\"/>\n",sid,tid,
	    t);
}

void
GR_write_start_path_logml_file(FILE *fp, int count)
{
    fprintf(fp,"<path count=\"%d\">\n",count);
}

void
GR_write_end_path_logml_file(FILE *fp)
{
    fprintf(fp,"</path>\n");
}
void
GR_write_start_user_session_logml_file(FILE *fp,char *name,char *referer,
				       char *epage,char *stime,long acount)
{
    fprintf(fp,"<userSession");
    if(name)
	fprintf(fp," name=\"%s\"",name);
    if(referer)
	fprintf(fp," ureferer=\"%s\"",referer);
    if(epage)
	fprintf(fp," entry_page=\"%s\"",epage);
    if(stime)
	fprintf(fp," start_time=\"%s\"",stime);
    fprintf(fp," access_count=\"%ld\">\n",acount);
}

void
GR_write_end_user_session_logml_file(FILE *fp)
{
    fprintf(fp,"</userSession>\n");
}
void
GR_write_start_user_sessions_logml_file(FILE *fp,int n, int max_edges,
					int min_edges)
{
    fprintf(fp,
	  "<userSessions count=\"%d\" max_edges=\"%d\" min_edges=\"%d\">\n",
	    n,max_edges,min_edges);
}

void
GR_write_end_user_sessions_logml_file(FILE *fp)
{
    fprintf(fp,"</userSessions>\n");
}
